﻿/*
 * fyyui.js
 * 范叶勇
 * fyybuilder.com
 * 2025-07-08 11:33:00 add
 * Copyright (c) since2015 泛依软件
 */
var queryListOptions = {};
var initAskPopupOptions;
$(function () {
    $(document).click(function (e) {
        selectHide(e);
    });

    if (document.body.clientWidth < 650) {
        var imgsrc = $(".f-header #ss").css("content");
        if (imgsrc != undefined) {
            imgsrc = imgsrc.replace('ss.png', 'zk.png');
            $(".f-header #ss").css("content", imgsrc);
            $('.f-navigator').toggleClass("f-hide");
            $('.f-bread-nav').toggleClass("f-left0px");
            $('.f-right-content').toggleClass("f-left0px");
        }
    }

    $(".f-shade").click(function () {
        toggleNavigator($('#ss_link')[0]);
    })

    $(".f-navigator .subNav").click(function () {
        $(this).toggleClass("curNav").siblings(".subNav").removeClass("curNav");
        $(this).next(".content").slideToggle(500).siblings(".content").slideUp(500);
    })

    $('.f-navigator ul li').click(function (event) {
        openBreadPage(this);
        if (document.body.clientWidth < 650) {
            toggleNavigator($('#ss_link')[0]);
        }
    });

    $('input[type=text]').prop('autocomplete', 'off');
});

function toggleNavigator(o) {
    var imgsrc = $(".f-header #ss").css("content");
    if (imgsrc.indexOf('ss.png') == -1) {
        imgsrc = imgsrc.replace('zk.png', 'ss.png');
        o.title = '收缩左侧菜单导航';
        $('.f-shade').removeClass('f-show');
        $('.f-shade').addClass('f-show');
    }
    else {
        imgsrc = imgsrc.replace('ss.png', 'zk.png');
        o.title = '展开左侧菜单导航';
        $('.f-shade').removeClass('f-show');
    }

    $(".f-header #ss").css("content", imgsrc);
    $('.f-header .name').toggleClass("f-hide");
    $('.f-header .nav').toggleClass("f-left0px");
    $('.f-navigator').toggleClass("f-hide");
    $('.f-bread-nav').toggleClass("f-left0px");
    $('.f-bread-nav').toggleClass("f-width100per");
    $('.f-right-content').toggleClass("f-left0px");
    $('.f-right-content').toggleClass("f-width100per");
}

function openBreadPage(nav) {
    $('.f-navigator li').removeClass('cur');
    $(nav).addClass('cur');
    var navid = $(nav).attr('f-navid');
    var url = $(nav).attr('f-url');
    var navname = $(nav).find('span').text();
    var breadnavel = $('.f-bread-nav ul #breadnav' + navid);
    if (breadnavel.is("*")) {
        //删除iframe
        $('#iframe' + navid).remove();
    } else {
        //添加面包屑
        var li = '<li id="breadnav' + navid + '" class="cur" f-frameid="iframe' + navid + '" onclick="breadnavOnclick(this)"><span>' + navname + '</span><img /></li>';
        $('.f-bread-nav ul').append(li);
        $('#breadnav' + navid + ' img').click(function (event) {
            event.stopPropagation(); // 阻止事件冒泡
            closebreadpage(this);
        });
    }

    var iframe = '<iframe id="iframe' + navid + '" style="overflow-x:auto !important;" class="f-iframe f-hide" src="' + url + '"></iframe>';
    $('.f-right-content').append(iframe);
    var height = $('.f-right-content').height();
    $(".f-iframe").css("height", height);
    $('.f-bread-nav ul #breadnav' + navid).trigger('click');
}

function breadnavOnclick(nav) {
    $('.f-bread-nav li').removeClass('cur');
    var navid = $(nav).attr('id').replaceAll('breadnav', '');
    $('.f-navigator li').removeClass('cur');
    $('li[f-navid="' + navid + '"]').addClass('cur');
    $('li[f-navid="' + navid + '"]').parent().slideDown(500).siblings('.content').slideUp(500);
    if (!$('li[f-navid="' + navid + '"]').parent().prev().hasClass('curNav')) {
        $('li[f-navid="' + navid + '"]').parent().prev().addClass('curNav');
    }
    $('li[f-navid="' + navid + '"]').parent().prev().siblings('.subNav').removeClass('curNav');
    $(nav).addClass('cur');
    $('.f-iframe').each(function () {
        if (!$(this).hasClass('f-hide')) {
            $(this).addClass('f-hide');
        }
    });
    var frameid = $(nav).attr('f-frameid');
    $('#' + frameid).removeClass('f-hide');
}

function closeBreadPage(img) {
    var frameid = $(img).parent().attr('f-frameid');
    var nextbreadnavel = $(img).parent().next("li");
    if (nextbreadnavel.length == 1) {
        nextbreadnavel.trigger('click');
    } else {
        var prevbreadnavel = $(img).parent().prev("li");
        if (prevbreadnavel.length == 1) {
            prevbreadnavel.trigger('click');
        }
    }

    $(img).parent().remove();
    $('#' + frameid).remove();
    $('li[f-navid="' + frameid.replaceAll('iframe', '') + '"]').removeClass('cur');
}

/**
 * 初始化列表
 * @param {any} options
 */
function initList(options) {
    var header = '<table align="center" border="0" cellpadding="0" cellspacing="0" class="f-table">';
    header += "<thead>";
    header += "<tr>";
    header += '<th field="{0}" style="min-width:60px">序号</th>';
    $.each(options.fields, function (index, field) {
        header += '<th field="' + field.code + '" format="' + (field.format ? "true" : "false") + '" style="min-width: ' + field.minWidth + 'px">' + field.name + '</th>';
    });

    header += '<th buttons="' + options.ops +'" style="width: 60px">...</th>';
    header += "</tr>";
    header += "</thead>";
    header += "</table>";
    header += "<div class='f-pagernav'></div>";
    $('#' + options.id).html(header);
    setListWidth();
}

/**
 * 设置列表宽度
 * 
 */
function setListWidth() {
    var con_len = $('.f-table-container').width();
    var tbl_len = $('.f-table-container .f-table').width();
    if (con_len > tbl_len) {
        $('.f-table-container .f-table').css("min-width", con_len - 10);
    }
}

/**
 * 查询列表
 * @param {any} options
 */
function queryList(options) {
    if (!(options.id in queryListOptions)) {
        queryListOptions[options.id] = options;
    }

    var props = {};
    options.pageindex = options.pageindex == undefined ? 1 : options.pageindex;
    props["pageindex"] = options.pageindex;
    var all_props = props;
    if (options.props != undefined) {
        all_props = mergeJsonObject(props, options.props);
    }

    if (options.url == '') {
        showError('未配置URL！', 3000);
        return;
    }

    var postjson = {
        type: "POST",
        async: "true",
        url: options.url,
        contentType: 'application/json',
        data: JSON.stringify(all_props),
        success: function (result) {
            $('.f-loading').hide();
            if (!result.success) {
                showError(result.message, 3000);
                return;
            }

            options.ds = result.data;
            options.total = result.total;
            options.pagesize = result.pageSize;
            renderList(options);
            renderPager(options);
            setListWidth();
        }
    }

    $.ajax(postjson);
}

/**
 * 渲染列表
 * @param {any} options
 */
function renderList(options) {
    var rowcount = $('#' + options.id + ' table tr').length;
    for (var i = 0; i < rowcount - 1; i++) {
        $('#' + options.id + ' tr:eq(1)').remove();
    }
    $.each(options.ds, function (rowIndex, item) {
        var header_row = $('#' + options.id + ' tr:eq(0)').clone();
        var ths = header_row.find("th");
        var newRow = $('<tr>');
        $.each(ths, function (k, v) {
            var field = $(v).attr('field');
            var buttons = $(v).attr('buttons');
            if (field != undefined) {
                var format = $(v).attr('format');
                var text = '';
                if (format == 'true' && options.fmt != undefined) {
                    text = options.fmt(item, field);
                } else {
                    text = field == "{0}" ? (rowIndex + 1) : item[field];
                }
                newRow.append($('<td>').append(text));
            } else if (buttons != undefined) {
                renderDropdown(options, rowIndex, newRow, buttons, item);
            }
        })
        newRow.mouseover(function () {
            $(this).addClass("over");
        }).mouseout(function () {
            $(this).removeClass("over");
        });
        $('#' + options.id + " table").append(newRow);
    });
}

/**
 * 渲染列表按钮下拉菜单
 * @param {列表查询选项} options
 * @param {列表行索引号} rowIndex
 * @param {列表行对象} tableRow
 * @param {按钮编码集合} buttons
 * @param {数据行对象} dsItem
 */
function renderDropdown(options,rowIndex, tableRow,buttons,dsItem) {
    var dropdownHtml = "<div class=\"f-dropdown\">";
    dropdownHtml += "    <a name=\"linkop\" href=\"#\" class=\"link-op\">...</a>";
    dropdownHtml += "    <div id=\"myDropdown" + rowIndex + "\" class=\"content op\">";
    dropdownHtml += "    </div>";
    dropdownHtml += "</div>";
    tableRow.append($('<td class="f-dropdown-container">').append(dropdownHtml));
    tableRow.find(".link-op").bind('click', { ID: rowIndex }, function (event) {
        toggleDropdown("myDropdown" + event.data.ID);
        return false;
    });

    var dropdown = tableRow.find(".f-dropdown .op");
    var btn_arr = buttons.split(',');
    for (var i = 0; i < btn_arr.length; i++) {
        var opcode = btn_arr[i];
        var op_option = null;
        if (options.ops != undefined) {
            for (var k = 0; k < options.ops.length; k++) {
                if (options.ops[k].code == opcode) {
                    op_option = options.ops[k];
                    break;
                }
            }
        }

        if (op_option != null) {
            dropdown.append("<a href=\"javascript:; \" class=\"" + opcode + "\">" + op_option.name + "</a>");
            var op_obj = dropdown.find("." + opcode);
            op_obj.bind('click', dsItem, function (event) {
                options.click(event.data, $(this).attr('class'));
                $('#myDropdown' + rowIndex).removeClass('show');
                return false;
            });
        }
    }
}

/**
 * 渲染分页
 * @param {any} options
 */
function renderPager(options) {
    var pagerContainer = $('#' + options.id).find('.f-pagernav');
    pagerContainer.html('');
    var pagecount = parseInt(options.total / options.pagesize);
    if (options.total % options.pagesize > 0) {
        pagecount++;
    }

    if (pagecount == 0 || pagecount == 1) {
        pagerContainer.append("<span class='note'>共" + options.total + "条记录</span>");
        return;
    }

    pagerContainer.append("<span class='note'>共" + options.total + "条记录<span class='curpage'>第" + options.pageindex + "页</span></span>");
    pagerContainer.append("<a page='1' href='javascript:;'>首页</a>");
    if (options.pageindex == 1) {
        pagerContainer.append("<a href='javascript:;'><<</a>");
    } else {
        pagerContainer.append("<a page='" + (options.pageindex - 1) + "' href='javascript:;'><<</a>");
    }
    var groupsize = 10;
    var groupindex = parseInt(options.pageindex / groupsize);
    if (options.pageindex % groupsize > 0) {
        groupindex++;
    }
    var navstr = "";
    for (var i = groupindex * groupsize - (groupsize - 1); i <= groupindex * groupsize && i <= pagecount; i++) {
        if (i == options.pageindex) {
            navstr += "<strong><span class='curpage'>" + i + "</span></strong>";
        }
        else {
            navstr += "<a page='" + i + "' class=\"pagelk\" href =\"javascript:;\">" + i + "</a>";
        }
    }

    pagerContainer.append(navstr);
    if (options.pageindex == pagecount) {
        pagerContainer.append("<a href='javascript:;'>>></a>");
    } else {
        pagerContainer.append("<a page='" + pagecount + "' href='javascript:;'>>></a>");
    }

    pagerContainer.append("<a page='" + pagecount + "' href='javascript:;'>尾页</a>");
    var pagenav_arr = pagerContainer.find("a");
    $.each(pagenav_arr, function (index, pagenav) {
        var page = $(pagenav).attr('page');
        if (page != undefined) {
            $(pagenav).bind('click', page, function (event) {
                options.pageindex = page;
                queryList(options);
                return false;
            });
        }
    });
}

/**
 * 初始化下拉列表
 * @param {any} options
 */
function initSelect(options) {
    if (options.valuefield != undefined) {
        $('#' + options.id).attr('f-valuefield', options.valuefield);
    }

    if (options.namefield != undefined) {
        $('#' + options.id).attr('f-namefield', options.namefield);
    }

    if (options.dictype != undefined) {
        $('#' + options.id).attr('f-dictype', options.dictype);
    }

    if (options.level == undefined) {
        options.level = "one";
    }

    $('#' + options.id).attr('f-level', options.level);
    if (options.namefield != undefined) {
        $('#' + options.id + ' cite').attr('f-namefield', options.namefield);
    }

    if (options.value == undefined) {
        options.value = "";
    }

    $('#' + options.id).attr('f-value', options.value );
    var selectHtml = '<cite f-whenempty="' + options.text + '">' + options.text + '</cite>';
    selectHtml += '<ul style="display: none;">';
    selectHtml += '<li><a href="javascript:;" f-value=""></a></li>';
    selectHtml += '</ul>';
    selectHtml += '</div>';
    $('#' + options.id).html(selectHtml);
    $('#' + options.id + " cite").click(function (e) {
        selectHide(e);
        var ul = $(this).parent().find('ul');
        ul.width($('#' + options.id).width());
        if (ul.css("display") == "none") {
            ul.slideDown("fast");
        } else {
            ul.slideUp("fast");
        }
    });
}

/**
 * 绑定下拉列表
 * @param {any} options
 */
function bindSelect(options) {
    var v = "#" + options.id;
    var selecttype = $(v).attr('f-dictype');
    var one_selectvalue = $(v).attr('f-value');
    if (selecttype == "$year$") {
        for (var y = 1900; y <= 2050; y++) {
            $(v).find('ul').append("<li" + (y == (new Date()).getFullYear() ? " class='scrolltome'" : "") + "><a href=\"javascript:;\" f-value=\"" + y + "\">" + y + "年</a></li>");
            if (one_selectvalue == y) {
                $(v).find('cite').html(y + "年");
            }
        }
    } else if (selecttype == "$month$") {
        for (var m = 1; m <= 12; m++) {
            $(v).find('ul').append("<li><a href=\"javascript:;\" f-value=\"" + m + "\">" + m + "月</a></li>");
            if (one_selectvalue == m) {
                $(v).find('cite').html(m + "月");
            }
        }
    } else {
        $.each(options.ds, function (index, item) {
            $(v).find('ul').append("<li><a href=\"javascript:; \" f-value=\"" + item.code + "\">" + item.name + "</a></li>");
            if (one_selectvalue == item.code) {
                $(v).find('cite').html(item.name);
            }
        });
    }

    $(v).find('ul li a').click(function () {
        var sel = $(this).parent().parent().parent();
        var ul = $(this).parent().parent();
        if (sel.attr('f-value') == $(this).attr("f-value")) {
            ul.hide();
            return;
        }

        sel.find('cite').html($(this).text());
        sel.attr('f-value', $(this).attr("f-value"));
        ul.hide();
        if ($(this).attr("f-value") == '') {
            sel.find('cite').html(sel.find('cite').attr('f-whenempty'));
        }
        var selectlevel = sel.attr('f-level');
        if (selectlevel == 'one') {
            //fyyobj.bindchildselect(sel, true);
        }
    });

    if (one_selectvalue != '') {
        //fyyobj.bindchildselect($(v), false);
    }
}

/**
 * 隐藏所有下拉列表
 * @param {any} e
 */
function selectHide(e) {
    if ($(".f-select").length == 0) {
        return;
    }

    var sels = $('.f-select');
    $.each(sels, function (k, v) {
        var sel = $(v)[0];
        var ul = $(v).find('ul');
        if (sel) {
            if (!sel.contains(e.target)) {
                ul.hide();
            }
        }
    });
}

/**
 * 初始化询问对话框
 * @param {any} options
 */
function initAskPopup(options) {
    initAskPopupOptions = options;
    var popupHTML = '';
    popupHTML += "   <div class=\"title\">" + options.title + "</div>";
    popupHTML += "   <div class=\"close\">";
    popupHTML += "       <a href=\"javascript:;\" title=\"关闭\">";
    popupHTML += "          <img src=\"/images/close.png\">";
    popupHTML += "       </a>";
    popupHTML += "   </div>";
    popupHTML += "   <div class=\"form\">";
    popupHTML += "       <div class=\"content\">" + options.content + "</div>";
    popupHTML += "   </div >";
    popupHTML += "   <div class=\"btn\" style=\"display: block; \">";
    popupHTML += "       <input type=\"button\" class=\"f-red-button\" value=\"是\">";
    popupHTML += "       <input type=\"button\" class=\"f-hidden\">";
    popupHTML += "       <input type=\"button\" class=\"f-ret-button\" value=\"否\">";
    popupHTML += "   </div>";
    $("#" + options.id).html(popupHTML);
    if (options.close != undefined) {
        $('#' + options.id).find('.close a').bind('click', {}, function (event) {
            options.close();
            hideAskPopup();
        });

        $('#' + options.id).find('.btn .f-ret-button').bind('click', {}, function (event) {
            options.close();
            hideAskPopup();
        });
    }

    if (options.ok != undefined) {
        $('#' + options.id).find('.btn .f-red-button').bind('click', {}, function (event) {
            options.ok(options.props);
            hideAskPopup();
        });
    }
}

/**
 *  显示询问框
 * @param {any} props
 */
function showAskPopup(props) {
    console.log(initAskPopupOptions);
    initAskPopupOptions.props = props;
    $('#' + initAskPopupOptions.id).css('display', 'block');
    $('.f-popup-shade').css('display', 'block');
}

/**
 * 隐藏询问框
 * */
function hideAskPopup() {
    $('#' + initAskPopupOptions.id).css('display', 'none');
    $('.f-popup-shade').css('display', 'none');
}

/**
 * 标签页显示
 * @param {any} tabindex
 */
function showTab(tabindex) {
    $('.f-form-tab .nav').removeClass('curnav');
    $('.f-form-tab .nav-' + tabindex).addClass('curnav');
    $('.fragment').each(function (index, item) {
        if (!$(this).hasClass('f-hide')) {
            $(this).addClass('f-hide');
        }
    });

    $('#fragment-' + tabindex).removeClass('f-hide');
}

/**
 * 绑定编辑页
 * @param {any} options
 */
function bindEditForm(options) {
    var props = buildProps(options);
    var all_props = props;
    if (options.props != undefined) {
        all_props = mergeJsonObject(props, options.props);
    }

    if (options.url == '') {
        showErr('未配置URL！', 3000);
        return;
    }

    var postjson = {
        type: "POST",
        async: "true",
        url: options.url,
        contentType: 'application/json',
        data: JSON.stringify(all_props),
        success: function (result) {
            $('.f-loading').hide();
            if (!result.success) {
                showError(result.message, 3000);
                return;
            }

            options.ds = result.data;
            var fieldobjs = $('*[f-formfield]');
            $.each(fieldobjs, function (k, v) {
                var obj_tag = $(v).prop('nodeName');
                var field = $(v).attr('f-formfield');
                var value = options.ds[field];
                if (obj_tag == 'DIV') {
                    $(v).attr("f-value", value);
                    if ($(v).hasClass("f-select")) {
                        var cite = $(v).find('cite');
                        $(v).find('ul li a').each(function () {
                            if (value == $(this).attr("f-value")) {
                                cite.html($(this).text());
                                return;
                            }
                        });
                    }
                } else {
                    $(v).val(options.ds[field]);
                }
            });
        }
    }

    $.ajax(postjson);
}

/**
 * 执行保存
 * @param {any} options
 */
function doSave(options) {
    var props = buildProps(options);
    var all_props = props;
    if (options.props != undefined) {
        all_props = mergeJsonObject(props, options.props);
    }

    if (options.url == '') {
        showErr('未配置URL！', 3000);
        return;
    }

    var postjson = {
        type: "POST",
        async: "true",
        url: options.url,
        contentType: 'application/json',
        data: JSON.stringify(all_props),
        success: function (result) {
            $('.f-loading').hide();
            if (!result.success) {
                showError(result.message, 3000);
                return;
            }

            options.ds = result.data;
            showSuccess("保存成功！", 3000);
        }
    }

    $.ajax(postjson);
}

/**
 * 执行删除
 * @param {any} options
 */
function doDelete(options) {
    var all_props = options.props;
    if (options.url == '') {
        showErr('未配置URL！', 3000);
        return;
    }

    var postjson = {
        type: "POST",
        async: "true",
        url: options.url,
        contentType: 'application/json',
        data: JSON.stringify(all_props),
        success: function (result) {
            $('.f-loading').hide();
            if (!result.success) {
                showError(result.message, 3000);
                return;
            }

            if (options.callback != undefined) {
                options.callback();
            }
        }
    }

    $.ajax(postjson);
}

/**
 * 构建属性数组
 * @param {any} options
 */
function buildProps() {
    var props = {};
    var fieldobjs = $("*[f-formfield]");
    $.each(fieldobjs, function (k, v) {
        var obj_tag = $(v).prop('nodeName');
        var formfield = $(v).attr("f-formfield");
        if (formfield != '') {
            if (obj_tag == 'DIV') {
                props[formfield] = $(v).attr("f-value");
            } else {
                props[formfield] = $(v).val().trim();
            }
        }
    });

    return props;
}

/**
 * 显示成功消息
 * @param {any} message
 * @param {any} life
 */
function showSuccess(message, life) {
    time = life;
    if ($(".f-tip").text().length > 0) {
        var msg = "<span>" + message + "</span>";
        $(".f-tip").empty().append(msg);
    } else {
        var msg = "<div class='f-tip'><span>" + message + "</span></div>";
        $("body").append(msg);
    }

    $(".f-tip").fadeIn(time);
    setTimeout(tip_fadout, time, time);
};

/**
 * 显示失败消息
 * @param {any} message
 * @param {any} life
 */
function showError(message, life) {
    showSuccess(message, life);
    $(".f-tip span").addClass("error");
};

function tip_fadout(time) {
    $(".f-tip").fadeOut(time);
};

window.onclick = function (event) {
    if (event.target.name != 'linkop') {
        hiddenDropdown("f-dropdown content");
    }
}

function toggleDropdown(id) {
    hiddenDropdown();
    $("#" + id).toggleClass('f-show');
}

function hiddenDropdown() {
    var dropdowns = $('.f-dropdown .content');
    var i;
    for (i = 0; i < dropdowns.length; i++) {
        var openDropdown = dropdowns[i];
        var classlist = openDropdown.className.split(/\s+/);
        for (var j = 0; j < classlist.length; j++) {
            if (classlist[j] === 'f-show') {
                $(openDropdown).removeClass('f-show');
            }
        }
    }
}

function gotoPage(url) {
    window.location.href = url;
}

function mergeJsonObject(jsonbject1, jsonbject2) {
    var resultJsonObject = {};
    for (var attr in jsonbject1) {
        resultJsonObject[attr] = jsonbject1[attr];
    }
    for (var attr in jsonbject2) {
        resultJsonObject[attr] = jsonbject2[attr];
    }
    return resultJsonObject;
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}

function getUrlVar(name) {
    var value = getUrlVars()[name];
    return value == undefined?'':value;
}